package com.ujf.GICOM_ldap;
import java.util.Hashtable;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

public class LdapWrapper {

	public static DirContext dirContext;

	public static DirContext getDirContext() {

		if (dirContext == null) {
			try {

				Hashtable<String, String> env = new Hashtable<String, String>();
				env.put(Context.INITIAL_CONTEXT_FACTORY,
						"com.sun.jndi.ldap.LdapCtxFactory");
				env.put(Context.PROVIDER_URL, "ldap://localhost:389");
				env.put(Context.SECURITY_AUTHENTICATION, "simple");
				env.put(Context.SECURITY_PRINCIPAL,
						"cn=Manager,dc=gicom,dc=org");
				env.put(Context.SECURITY_CREDENTIALS, "secret");
				// Context context = new InitialContext(env);

				dirContext = new InitialDirContext(env);
				return dirContext;

			} catch (NamingException e) {
				e.printStackTrace();
				return null;
			}
		}
		return dirContext;

	}
}
